/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultForm.java
 *
 * Created on 25-gen-2011, 17.08.43
 */

package mediawheregui;

import javax.swing.table.*;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JFrame;
import java.awt.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Collection;
import java.util.Iterator;

import org.slf4j.impl.*;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrDocument;
import org.apache.commons.codec.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.io.*;
import org.apache.commons.logging.*;

/**
 *
 * @author gioak
 */
public class ResultForm extends JFrame {

    /** Creates new form ResultForm */
    public ResultForm(ArrayList<String> result, SolrDocumentList docTot, String find) {
        initComponents();

        String homePath = System.getenv("HOME");
        String logoPath = homePath + "/.mediawhere/images/logo.jpeg";
        Image logo = new ImageIcon(logoPath, "logo").getImage();
        this.setIconImage(logo);

        this.resultNumber = result.size();
        this.docTot = docTot;
        this.setTitle("Result Search For: " + find);

        //this.table = jTable1;


        //this.validate();
        //jTable1.validate();
        //jTextArea1.validate();
        //jTable1.getSelectionModel().addListSelectionListener(new RowListener());
        this.setVisible(true);
        jTable1.setVisible(true);
        if(result.isEmpty()==true)
        jTable1.setValueAt("NO RESULTS", 0, 0);
        else
        {
            jTable1.setModel(new DefaultTableModel(new Object [result.size()][1], new String [] {"Path file found"}));

            for(int i=0; i<result.size(); i++)
                jTable1.setValueAt(result.get(i), i, 0);

            
        }


    }

       /* private class RowListener implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent event) {

            }
            //output.append("ROW SELECTION EVENT. ");
            //outputSelection();
        }*




    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tablePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Results Search");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Path files found"
            }
        ));
        jTable1.setColumnSelectionAllowed(true);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        javax.swing.GroupLayout tablePanelLayout = new javax.swing.GroupLayout(tablePanel);
        tablePanel.setLayout(tablePanelLayout);
        tablePanelLayout.setHorizontalGroup(
            tablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1003, Short.MAX_VALUE)
        );
        tablePanelLayout.setVerticalGroup(
            tablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(tablePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 1003, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tablePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tablePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        for(int i=0; i<resultNumber; i++)
        {
            if(jTable1.isRowSelected(i)==true)
            {
                //System.out.println(i);
                SolrDocument doc = new SolrDocument();
                String pathSelected = (String) jTable1.getValueAt(i, 0);
                String nameFile = new String();
                StringTokenizer strTok = new StringTokenizer(pathSelected, "/");
                while (strTok.hasMoreTokens()) {
                        nameFile = strTok.nextToken();
                    }
                for(int j=0; j<docTot.size(); j++)
                {
                    doc = docTot.get(i);
                    Collection<String> list = doc.getFieldNames();
                    Iterator<String> it = list.iterator();
                    String temp = new String();

                    while (it.hasNext()) {
                        temp = it.next();
                        if(temp.equals("attr_filename"))
                        {
                            String tagFile = new String();
                            Iterator<String> it1 = list.iterator();
                            String tmp = new String();

                            while(it1.hasNext())
                            {
                                tmp = it1.next();
                                if (doc.get(tmp).getClass() != tmp.getClass())//controllo se il valore del campo è una Stringa
                                {
                                    continue;
                                }
                                String value = (String)doc.getFieldValue(tmp);
                                tagFile = tagFile + "[" + tmp + "]" + ":  " + value + "\n";

                            }
                            jTextArea1.setText(tagFile);
                        }


                }
            }
        }
    }//GEN-LAST:event_jTable1MouseClicked
    }

    public void paintAll(Graphics g){
        super.paintAll(g);

        Dimension d = this.getSize();
        this.tablePanel.setSize(d);

    }



    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ResultForm().setVisible(true);
            }
        });
    }*/



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JPanel tablePanel;
    // End of variables declaration//GEN-END:variables
    private int resultNumber;
    private SolrDocumentList docTot;

    /*public JPanel getJpanel1(){
        return this.jPanel1;
    }
    public JPanel getTablePanel(){
        return this.tablePanel;
    }
    public JTable getJtable1(){
        return this.jTable1;
    }
    public JTextArea getJtextArea(){
        return this.jTextArea1;
    }
    public void setJpanel1(JPanel panel){
        this.jPanel1 = panel;
    }
    public void setTablePanel(JPanel tablePanel){
        this.tablePanel = tablePanel;
    }
    public void setJtable1(JTable table){
        this.jTable1 = table;
    }
    public void setJtextArea(JTextArea textArea){
        this.jTextArea1 = textArea;
    }*/
}



   /*  class MyFrame extends javax.swing.JFrame {

         private JPanel panel1;
        private JPanel tablePanel;
        public JTable table;
        private JTextArea area;

       /* public MyFrame (){
            
        }*/

   /* public MyFrame (ResultForm fr){

        panel1 = fr.getJpanel1();
        tablePanel = fr.getTablePanel();
        table = fr.getJtable1();
        area = fr.getJtextArea();

    }*/

  /*  public void paintComponents(Graphics g){
        super.paintComponents(g);
        Dimension d = this.getSize();

        //double height = d.getHeight();
        //double width = d.getWidth();

        //panel1.setSize(d);
        //tablePanel.setSize(d);
        //area.setSize(d);
        table.setSize(d);
        

    }
} */